#!/usr/bin/python
# -*- coding: utf-8 -*-

import Image
import urllib
import socket
import MySQLdb
import StringIO

socket.setdefaulttimeout(20)

def his_inter(h1, h2):
    up = 0
    down = 0
    down2 = 0
    for i in range(0, 768):
    	up = up + min(h1[i], h2[i])
    	down = down + h1[i]
    	down2 = down2 + h2[i]
    return min(1.0 * up / down, 1.0 * up / down2)

def duplicate(thumb1, thumb2, duration1, duration2):
    if abs(duration2 - duration1) > 3:
    	return False
    return his_inter(thumb1.histogram(), thumb2.histogram()) > 0.8

def get_second_count(t):
    return 3600 * (int)(t[0:2]) + 60 * (int)(t[3:5]) + (int)(t[6:8])

def deduplicate(l1, l2):
    thumbnails = []
    video_cnt = len(l1[2])
    for i in range(0, video_cnt):
        try:
            data = urllib.urlopen(l1[3][i]).read()
            print l1[3][i]
            thumbnails.append(Image.open(StringIO.StringIO(data)))
        except Exception, why:
            thumbnails.append('nothing')
            print why
            continue
        label = True
        for j in range(0, i):
            try:
                if duplicate(thumbnails[j], thumbnails[i], get_second_count(l1[5][j]), get_second_count(l1[5][i])):
                    label = False
                    print j, i
                    break
            except Exception, why:
                print why
        if label:
            for j in range(0, 6):
            	l2[j].append(l1[j][i])

# 连接数据库
conn = MySQLdb.Connection("172.16.7.58", "root", "researchsucks", "X", 3306)
conn.set_character_set("utf8");
cur = conn.cursor()

# 读取候选列表及缩略图
def get_second_count(t):
    return (int)(t[0:2]) * 3600 + (int)(t[3:5]) * 60 + (int)(t[6:8])
file = open('/home/chaojiansong/videobook/notinsvn/video_score_list', 'r')
id_score_link_image_title_duration = [[], [], [], [], [], []]
id_score_link_image_title_duration_uniq = [[], [], [], [], [], []]
cnt = 0
for line in file:
    id_score_link_image_title_duration[cnt % 6].append(line.rstrip())
    cnt = cnt + 1
# 去重
deduplicate(id_score_link_image_title_duration, id_score_link_image_title_duration_uniq)

# 写结果
out = open('/home/chaojiansong/videobook/notinsvn/video_score_list.uniq', 'w')
cnt = len(id_score_link_image_title_duration_uniq[0])
for i in range(0, cnt):
    for j in range(0, 6):
    	out.write((str)(id_score_link_image_title_duration_uniq[j][i]) + '\n')

out.close()
file.close()
cur.close()
conn.close()
